//新增
function addPlacementBaseInfo() {
    var data = {stat: 1};
    if (data) {
        showPlacementBaseInfoWindow(data, 0,0);
    }
}

function editPlacementBaseInfo() {
    var row = grid.getSelected();
    if (row) {
        $.ajax({
            url: "/user/getuser/getLoginUserDepartId",
            type: "get",
            cache: false,
            async: false,
            success: function (success) {
                if (success.code == 1) {
                    var loginUserDepartId = success.data;
                    if (row.clab == loginUserDepartId) {
                        showPlacementBaseInfoWindow(row, 0,1);
                    } else {
                        showPlacementBaseInfoWindow(row, 1,1);
                    }
                } else {
                    return null;
                }
            }

        })
    } else {
        mini.alert("请至少选择一条数据！");
    }
}

// 查看页面 - 安帮基地信息
function checkPlacementBaseInfo(){
    var row = grid.getSelected();
    if (row) {
        showPlacementBaseInfoWindow(row, 1,-1);
    } else {
        mini.alert("请至少选择一条数据！");
    }
}

function showPlacementBaseInfoWindow(row, flog,flag) {
    commonEdit("/azbj/jdxxgl/placementBaseInfo/edit/" + flog+"/flag/"+flag, "安帮基地信息", row, "55%", "67%", true, false, true, callback);

    function callback() {
        grid.reload();
    }
}

function delPlacementBaseInfo() {
    var row = grid.getSelected();
    if (row) {
        $.ajax({
            url: "/user/getuser/getLoginUserDepartId",
            type: "get",
            cache: false,
            async: false,
            success: function (success) {
                if (success.code == 1) {
                    var loginUserDepartId = success.data;
                    if (row.clab == loginUserDepartId) {
                        commonRemove("/azbj/jdxxgl/placementBaseInfo/delete/" + row.id, row, {_method: "delete"}, callback);
                        function callback() {
                            grid.reload();
                        }
                    } else {
                        mini.alert("非本机构添加的内容无法进行删除");
                    }
                } else {
                    return null;
                }
            }

        })
    } else {
        mini.alert("请至少选择一条数据！");
    }
}


//格式化日期函数
function onDateRenderer(e) {
    var date = new Date(e.value);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    var formatTime = Y + M + D + h + m + s;
    return formatTime;
}

//基地类型类别转化
function onChangePlaceTypeCode(e) {
    var placeType = e.value;
    if (placeType == 1) {
        return "企业认证基地";
    } else if (placeType == 2) {
        return "政府投入为主的过渡性安置基地";
    } else if (placeType == 3) {
        return "福利机构创办的安置基地";
    } else if (placeType == 4) {
        return "民政部门创办的安置基地";
    } else {
        return "其他";
    }
}

//救助类型类别转化
function onChangeHelpTypeCode(e) {
    var helpType = e.value;
    if (helpType == 1) {
        return "重点帮教对象";
    } else if (helpType == 2) {
        return "一般帮教对象";
    } else if (helpType == 3) {
        return "全部";
    } else {
        return "其他";
    }
}

//转换为年月日
function onYearMonthDayDateRenderer(e) {
    var date = new Date(e.value);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    var formatTime = Y + M + D;
    return formatTime;
}

//批量导出
function exportPlaceBaseExcelFile(e) {
    var num = grid.getData().length;
    if (num > 0) {
        top.location.href = encodeURI("/azbj/abwordresource/common/exportExcelFile?type=2");
    } else {
        mini.alert("没有数据可导出");
    }
}

/**
 * 动态渲染列中按钮
 * @param e
 * @returns {string}
 */
function doPlacementBaseInfo(e){
    var s = "";
    var record = e.row.showStatus;
    var arr = record.split("@");
    var showStatus = arr[0];
    var clab = arr[1];
    var departId = arr[2];
    if(e.row.localCode == '140000'){
        if(clab == departId) {
            if(showStatus == 1){
                s += '&nbsp;<a class="mini-button mini-button-warning"  onclick="isShow()"  >&nbsp;&nbsp;禁用&nbsp;&nbsp;</a>&nbsp';
            }else if(showStatus == 2){
                s += '&nbsp;<a class="mini-button mini-button-info"  onclick="isShow()"  >&nbsp;&nbsp;启用&nbsp;&nbsp;</a>&nbsp';
                s += '&nbsp;<a class="mini-button mini-button-primary"  onclick="editPlacementBaseInfo()"  >&nbsp;&nbsp;编辑&nbsp;&nbsp;</a>&nbsp';
                s += '&nbsp;<a class="mini-button mini-button-danger"  onclick="delPlacementBaseInfo()"  >&nbsp;&nbsp;删除&nbsp;&nbsp;</a>&nbsp';
            }
        }
        s += '&nbsp;<a class="mini-button mini-button-info"  onclick="checkPlacementBaseInfo()"  >&nbsp;&nbsp;查看&nbsp;&nbsp;</a>&nbsp';
    }
    return s;
}

//修改展示状态
function isShow() {
    var row = grid.getSelected();
    var arr = row.showStatus;
    var status = arr[0];
    if(status == 1){
        status = 2;
    }else{
        status = 1;
    }
    //判断这个基地下有没有安置服刑人员
    var message = "确定要修改基地展示的状态吗？";
    $.ajax({
        url: '/azbj/jdxxgl/placementBaseInfo/getBaseHasPm/'+ row.id,
        type: "get",
        success: function (res) {
            if(res.data > 0 && status == 2){
                mini.alert("请将基地人员移出后选择禁用!!!");
            }else{
                var url = "/azbj/jdxxgl/placementBaseInfo/isShows/" + row.id+"/"+status;
                if (row) {
                    mini.confirm(message,
                        "确定？",
                        function (action) {
                            if (action == "ok") {
                                $.ajax({
                                    url: url,
                                    type: "post",
                                    success: function (text) {
                                        if(text.code == 1){
                                            mini.alert("修改成功! ");
                                            callback();
                                        }else if(text.code==4000){
                                            mini.alert(text.message);
                                        }
                                    },
                                    error: function () {
                                        mini.alert("修改失败! ");
                                    }
                                });
                            }
                        });
                    function callback() {
                        grid.reload();
                    }
                } else {
                    mini.alert("请至少选中一条记录");
                }
            }
        },
    });
}